package com.tedu.controller;

import java.io.IOException;
import java.io.Writer;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; 
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.NativeWebRequest;

import com.tedu.model.Address;
import com.tedu.model.Cart;
import com.tedu.model.Product;
import com.tedu.model.User;
import com.tedu.service.IAddressService;
import com.tedu.service.ICartService;
import com.tedu.service.IProductService;
import com.tedu.service.impl.ProductService;

@Controller
@RequestMapping("/cart")
public class CartController {
	private ICartService cartService;
	private IAddressService addressService;
	private IProductService productService;
	
	
	
	@Resource
	public void setCartService(ICartService cartService) {
		this.cartService = cartService;
	}
	@Resource
	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	@Resource
	public void setAddressService(IAddressService addressService) {
		this.addressService = addressService;
	}

	/**
	 * 添加到购物车操作
	 */
	@RequestMapping(value="/addCart", method=RequestMethod.POST)
	public void addCart(int id, int quantity, HttpServletResponse resp, HttpSession session) throws IOException {
		//获取登录用户
		User user = (User) session.getAttribute("loginUser");
		//判断用户是否登录
		if(user == null) {
			session.setAttribute("URI", "product/" + id + "/item");
			resp.sendRedirect("");
			return;
		}
		else {
			//获取系统时间
			Date d = new Date();
			//格式化时间
			String fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
			//时间转换
			Timestamp date = Timestamp.valueOf(fmt);
			//创建商品
			Product product = new Product();
			product.setId(id);
			Cart cart = new Cart(user, product, quantity, date);
			cartService.add(cart);
			int cartSize = cartService.cartSize(user.getId());
			session.setAttribute("cartSize", cartSize);
			Writer out = null;
			try {
				out = resp.getWriter();
				out.write("yes");
			} finally {
				out.close();
			}
		}
	}
	@RequestMapping(value="/my_cart", method=RequestMethod.GET)
	public String myCart(Model model, HttpSession session) {
		User user = (User) session.getAttribute("loginUser");
		//判断用户是否登录
		if(user == null) {
			return "redirect:/user/login";
		}
		List<Cart> carts = cartService.list(user.getId());
		model.addAttribute("carts", carts);
		return "client/user/my_cart";
	}
	
	/**
	 * 修改购买数量
	 * @throws IOException 
	 */
	@RequestMapping(value="/updateQuantity", method=RequestMethod.PUT)
	public void updateQuantity(int cid, String option, Integer quentity,HttpServletResponse resp, HttpSession session, Model model) throws IOException {
		User user = (User) session.getAttribute("loginUser");
		Cart cart = cartService.load(cid);
		if(option.equals("plus")) {
			cart.setQuantity(quentity);
		}
		if(option.equals("minus")) {
			cart.setQuantity(quentity);
		}
		
		if(option.equals("xiugai")) {
			cart.setQuantity(quentity);
		}
		cartService.update(cart);
		List<Cart> carts = cartService.list(user.getId());
		model.addAttribute("carts", carts);
		Writer out = null;
		try {
			out = resp.getWriter();
			out.write("yes");
		} finally {
			out.close();
		}
	}
	
	/**
	 * 购物车结算
	 */
	@RequestMapping(value="/settlement", method=RequestMethod.POST)
	public String settlement(int cartids[], Model model, HttpSession session) {
		User loginUser = (User) session.getAttribute("loginUser");
		//获取登录用户所有的地址信息
	
		List<Address> addresses = addressService.list(loginUser.getId());
		List<Cart> carts = new ArrayList<Cart>();
		for(int i = 0; i < cartids.length; i++) {
			Cart cart = cartService.load(cartids[i]);
			carts.add(cart);
		}
		model.addAttribute("addresses", addresses);
		model.addAttribute("carts", carts);
		//条转到确认订单页面
		return "client/user/buy_now";
	}
	/**
	 * 批量删除购物车记录
	 */
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public String delete(int cartids[], HttpSession session) {
		for(int i = 0; i < cartids.length; i++) {
			cartService.delete(cartids[i]);
		}
		User loginUser = (User) session.getAttribute("loginUser");
		int cartSize = cartService.cartSize(loginUser.getId());
		session.setAttribute("cartSize", cartSize);
		return "redirect:/cart/my_cart";
	}
	/**
	 * 删除一条购物车记录
	 */
	@RequestMapping(value="/delete/{cartid}", method=RequestMethod.GET)
	public String delete(@PathVariable int cartid, HttpSession session) {
		cartService.delete(cartid);
		User loginUser = (User) session.getAttribute("loginUser");
		int cartSize = cartService.cartSize(loginUser.getId());
		session.setAttribute("cartSize", cartSize);
		return "redirect:/cart/my_cart";
	}
	
	/**
	 * 
	 * 立即购买结算
	 */
	/**
	 * 购物车结算
	 */
	@RequestMapping(value = "/nowsettlement", method = RequestMethod.POST)
	public String nowsettlement( Integer id,  int  quantity ,Model model, HttpSession session) {
		User loginUser = (User) session.getAttribute("loginUser");		
		// 获取登录用户所有的地址信息
		List<Address> addresses = addressService.list(loginUser.getId());
		List<Cart> carts = new ArrayList<Cart>();
		Date d = new Date();
		//格式化时间
		String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
		//时间转换
		Timestamp date = Timestamp.valueOf(nowTime);
		//创建订单
		Product product=productService.productItem(id);
		 Cart cart = new Cart(loginUser,product,quantity,date);		
	/*	cartService.add(cart);*/
		 carts.add(cart);	
		model.addAttribute("addresses", addresses);
		model.addAttribute("carts", carts);
		model.addAttribute("quanties", quantity );
		// 条转到确认订单页面
		return "client/user/buynow";
	}	
	

	
	
	
}
